package roborally.program.conditions;

import java.util.Iterator;

import roborally.model.Element;
import roborally.model.Item;
import roborally.model.Robot;

/**
 * A class for dealing with at-item-conditions: (at-item).  
 * 
 * @version	1.0
 * @author  Pieter Robberechts	(1e Bach Informatica)
 * @author	Jeroen Sanders		(1e Bach Informatica)
 *
 */
public class AtItem extends Condition {

	/**
	 * Initialize this new at-item-condition with a line number.
	 *
	 */
	public AtItem(int lineNb) {
		super(lineNb);
	}

	/**
	 * Evaluate this condition for a given robot.
	 * 
	 * @return	Return true if and only if the given robot shares his position with
	 * 			an item; else return false.
	 * 			| for one element in robot.getBoard().getElements(robot.getPosition()):
	 * 			|	if(element instanceof Item) 
	 * 			|		then result == super.evaluate(robot)
	 * 			|	else then result == false
	 */
	@Override
	public boolean evaluate(Robot robot) {
		Iterator<Element> elementsOnPositionRobot = robot.getBoard().getElements(robot.getPosition()).iterator();
		while(elementsOnPositionRobot.hasNext()) 
			if(elementsOnPositionRobot.next() instanceof Item)
				return super.evaluate(robot);
		return false;
	}

	/**
	 * Return a textual representation of this at-item-condition,
	 * formatted in accordance with its position in the program.
	 * 
	 * @return	The result is a pretty formatted string of this at-item-condition,
	 * 			taking in account his relative position in the program.
	 * 			| result == getIndentation(indentationLevel) + "(at-item)"
	 */
	@Override
	public String prettyFormat(int indentationLevel) {
		return getIndentation(indentationLevel) + "(at-item)";
	}

}
